﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Model;
using DBServer;

namespace StudyLibrary.MenuForm
{
    public partial class frmMenuDetail : Form
    {
        public bool _isSuccess = false;

        int _type = 0;//0:add   1:update

        MENU _menu;
        public frmMenuDetail()
        {
            InitializeComponent();
        }

        public frmMenuDetail(int type, MENU menu)
        {
            InitializeComponent();
            this._menu = menu;
            this._type = type;
        }

        private void frmMenuDetail_Load(object sender, EventArgs e)
        {
            if (_type==1)
            {
                txt_Name.Text = _menu.Name;
                txt_Path.Text = _menu.Path;
                numericUpDown1.Value = _menu.ViewOrder;

                txt_Name.Enabled = false;
            }
        }

        private void btn_Sure_Click(object sender, EventArgs e)
        {
            string _name = txt_Name.Text.Trim();
            string _path = txt_Path.Text.Trim();
            int _order = Convert.ToInt32(numericUpDown1.Value);
            if (string.IsNullOrEmpty(_name)||string.IsNullOrEmpty(_path))
            {
                MessageBox.Show("information can't empty");
                return;
            }

            MENU _m = new MENU();
            _m.Name = _name;
            _m.Path = _path;
            _m.ViewOrder = _order;

            if (_type == 0)
            {
                //string _sqlAdd = "INSERT INTO Menu VALUES('" + _name + "','" + _path + "'," + _order + ")";
                //int re = SQLiteHelper.ExecuteNonQuery(_sqlAdd);
                int re = NHibernateHelper.SaveObject(_m);
                if (re > 0)
                {
                    MessageBox.Show("Successful");
                    _isSuccess = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Failure");
                }
            }
            else if (_type == 1)
            {
                //string _sqlUpdate = "UPDATE Menu SET Path = '" + _path + "',ViewOrder = " + _order + " WHERE Name = '" + _name + "'";
                //int re = SQLiteHelper.ExecuteNonQuery(_sqlUpdate);
                int re = NHibernateHelper.UpdateObject(_m);
                if (re > 0)
                {
                    MessageBox.Show("Successful");
                    _isSuccess = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Failure");
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
